﻿@model AdminEditViewModel
@using Orchard.Projections.ViewModels;

@{
    Layout.Title = T("Edit Query - {0}", Model.Name);
    Style.Include("admin-projections.css");
}

@using (Html.BeginFormAntiForgeryPost()) {
    @Html.ValidationSummary()

    <fieldset class="bulk-actions">
        <h2>@T("Filters")</h2>
    </fieldset>

    <div class="manage">
        @Html.ActionLink(T("Add a new Group").Text, "AddGroup", new { controller = "Filter", id = Model.Id }, new { @class = "button primaryAction" })
    </div>

    foreach (var group in Model.FilterGroups) {
        bool isFirstGroup = group == Model.FilterGroups.First();
        if (!isFirstGroup) {
            <h2>@T("Or")</h2>
        }

        <table class="items">
            <thead>
                <tr>
                    <th scope="col">@T("Description")</th>
                    <th scope="col" class="actions">
                        <ul class="action-links">
                            <li class="action-link">
                                @Html.ActionLink(T("+ Add a new Filter").Text, "Add", new { controller = "Filter", id = group.Id })
                            </li>
                            @if (Model.FilterGroups.Count() > 1) {
                                <li class="action-link">
                                    @Html.ActionLink(T("Delete group").Text, "DeleteGroup", new { controller = "Filter", id = group.Id }, new { itemprop = "RemoveUrl UnsafeUrl" })
                                </li>
                            }
                        </ul>
                    </th>
                </tr>
            </thead>
            @foreach (var filter in group.Filters) {
                <tr>
                    <td>@filter.DisplayText</td>
                    <td>
                        <ul class="action-links">
                            <li class="action-link">
                                @Html.ActionLink(T("Edit").Text, "Edit", new { controller = "Filter", id = group.Id, category = filter.Category, type = filter.Type, filterId = filter.FilterRecordId })
                            </li>
                            <li class="action-link">
                                @Html.ActionLink(T("Delete").Text, "Delete", new { controller = "Filter", id = Model.Id, filterId = filter.FilterRecordId }, new { itemprop = "RemoveUrl UnsafeUrl" })
                            </li>
                        </ul>
                    </td>
                </tr>
            }
        </table>
    }

    <fieldset class="bulk-actions">
        <h2>@T("Sort Criteria")</h2>
    </fieldset>

    <table class="items">
        <thead>
            <tr>
                <th scope="col">@T("Description")</th>
                <th scope="col" class="actions">@Html.ActionLink(T("+ Add a new Sort Criteria").Text, "Add", new { controller = "SortCriterion", id = Model.Id })</th>
            </tr>
        </thead>
        @foreach (var sortCriterion in Model.SortCriteria) {
            <tr>
                <td>@sortCriterion.DisplayText</td>
                <td>
                    <ul class="action-links">
                        <li class="action-link">
                            @Html.ActionLink(T("Edit").Text, "Edit", new { controller = "SortCriterion", id = Model.Id, category = sortCriterion.Category, type = sortCriterion.Type, sortCriterionId = sortCriterion.SortCriterionRecordId })
                        </li>
                        <li class="action-link">
                            @Html.ActionLink(T("Delete").Text, "Delete", new { controller = "SortCriterion", id = Model.Id, sortCriterionId = sortCriterion.SortCriterionRecordId }, new { itemprop = "RemoveUrl UnsafeUrl" })
                        </li>
                        <li class="action-link">
                            @Html.ActionLink(T("Up").Text, "Move", new { controller = "SortCriterion", id = sortCriterion.SortCriterionRecordId, direction = "up", queryId = Model.Id })
                        </li>
                        @if (sortCriterion != Model.SortCriteria.Last()) {
                            <li class="action-link">
                                @Html.ActionLink(T("Down").Text, "Move", new { controller = "SortCriterion", id = sortCriterion.SortCriterionRecordId, direction = "down", queryId = Model.Id })
                            </li>
                        }
                    </ul>
                </td>
            </tr>
        }
    </table>

    <fieldset class="bulk-actions">
        <h2>@T("Layouts")</h2>
    </fieldset>

    <table class="items">
        <thead>
            <tr>
                <th scope="col">@T("Description")</th>
                <th scope="col" class="actions">@Html.ActionLink(T("+ Add a new Layout").Text, "Add", new { controller = "Layout", id = Model.Id })</th>
            </tr>
        </thead>
        @foreach (var layout in Model.Layouts) {
            <tr>
                <td>@(layout.DisplayText)</td>
                <td>
                    <ul class="action-links">
                        <li class="action-link">
                            @Html.ActionLink(T("Edit").Text, "Edit", new { controller = "Layout", id = layout.LayoutRecordId })
                        </li>
                        <li class="action-link">
                            @Html.ActionLink(T("Delete").Text, "Delete", new { controller = "Layout", id = layout.LayoutRecordId }, new { itemprop = "RemoveUrl UnsafeUrl" })
                        </li>
                    </ul>
                </td>
            </tr>
        }
    </table>

    @* Render a button at the bottom only if there are several layouts in the table *@
    if (Model.Layouts.Count() > 5) {
        <div class="manage">
            @Html.ActionLink(T("Add a new Layout").Text, "Add", new { controller = "Layout", id = Model.Id }, new { @class = "button primaryAction" })
        </div>
    }

    <fieldset>
        @if (Model.FilterGroups.SelectMany(x => x.Filters).Any()) {
            @Html.ActionLink(T("Preview").ToString(), "Preview", new { Model.Id }, new { @class = "button" });
        }
        @Html.ActionLink(T("Close").ToString(), "Index", new { }, new { @class = "button" })
    </fieldset>
}